<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryRef" inline>
          <el-form-item label="试卷名称" prop="paperName">
            <el-input
              v-model="queryParams.paperName"
              placeholder="请输入试卷名称"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table
          v-loading="loading"
          :data="paperList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            label="学科"
            align="center"
            key="subjectName"
            prop="subjectName"
            show-overflow-tooltip
          />
          <el-table-column
            label="试卷名称"
            align="center"
            key="paperName"
            prop="paperName"
            show-overflow-tooltip
          />
          <el-table-column label="参考班级" align="center">
            <template #default="scope">
              <span>{{
                scope.row.classInfoList.map((item) => item.className).toString()
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="考试时长" align="center">
            <template #default="scope">
              <span>{{ scope.row.duration }}分钟</span>
            </template>
          </el-table-column>
          <el-table-column
            label="创建时间"
            align="center"
            prop="creatTime"
            width="160"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                type="primary"
                link
                icon="Edit"
                @click="handleStartTest(scope.row)"
                >查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 自动组卷对话框 -->
    <el-dialog title="自动组卷" v-model="open" width="800px" append-to-body>
      <el-form :model="form" :rules="rules" ref="combineRef" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="学科" prop="subId">
              <el-select
                v-model="form.subId"
                placeholder="请选择"
                style="width: 100%"
                @change="subjectChange"
              >
                <el-option
                  v-for="item in subjectOptions"
                  :key="item.id"
                  :label="item.subject"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="知识点" prop="knowledgeId">
              <el-select
                v-model="form.knowledgeId"
                placeholder="请选择"
                style="width: 100%"
                multiple
              >
                <el-option
                  v-for="item in knowledgeOptions"
                  :key="item.id"
                  :label="item.knowledge"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="参试班级" prop="classIdList">
              <el-select
                v-model="form.classIdList"
                placeholder="请选择"
                style="width: 100%"
                multiple
              >
                <el-option
                  v-for="item in classList"
                  :key="item.id"
                  :label="item.className"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="建议时长" prop="duration">
              <el-input-number
                v-model="form.duration"
                min="1"
              ></el-input-number>
            </el-form-item>
            <el-form-item
              v-for="item in form.topicTypeList"
              :label="item.label"
              :key="item.id"
            >
              <el-col :span="6">
                <el-input-number v-model="item.value" min="1"></el-input-number>
              </el-col>
              <el-col :span="6">
                <el-button type="text" @click="RemoveTopic(item)"
                  >删除</el-button
                >
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="addTopic">添加题型</el-button>
          <el-button type="primary" @click="autoCombineConfirm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog
      title="题型选择"
      v-model="topicOpen"
      width="400px"
      append-to-body
    >
      <el-form
        :model="topicForm"
        :rules="topicRules"
        ref="topicRef"
        label-width="80px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="题型" prop="topicid">
              <el-select
                v-model="topicForm.topicid"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in topicTypeOption"
                  :key="item.id"
                  :label="item.label"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="topicSelectConfirm"
            >确 定</el-button
          >
          <el-button @click="topciCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Class">
import { getTeacherExam, getStudentExam } from "@/api/core/test";
import { getSubjectList } from "@/api/core/subject";
import { getTopicList } from "@/api/core/topic";
import { getSchoolClassList } from "@/api/core/class";
import {
  getCurrentInstance,
  reactive,
  ref,
  toRefs,
  watch,
  computed,
} from "vue";
import { parseTime } from "@/utils/ruoyi";
import useUserStore from "@/store/modules/user";

const { proxy } = getCurrentInstance();
const router = useRouter();
const paperList = ref([]);
const loading = ref(true);
const total = ref(0);
const userStore = useUserStore();
const isStudent = computed(() => useUserStore().isStudent);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
});
const { queryParams } = toRefs(data);

/** 查询试卷列表 */
function getList() {
  loading.value = true;
  if (isStudent.value) {
    getStudentExam(
      Object.assign({ stuId: userStore.userId }, queryParams.value)
    ).then((res) => {
      loading.value = false;
      paperList.value = res.rows;
      total.value = res.total;
    });
  } else {
    getTeacherExam(queryParams.value).then((res) => {
      loading.value = false;
      paperList.value = res.rows;
      total.value = res.total;
    });
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 开始考试
function handleStartTest(row) {
  router.push({
    path: "/result/detail",
    query: { paperId: row.id, allscore: row.allscore },
  });
}

getList();
</script>

<style scoped lang="scss"></style>
